// import com.alibaba.druid.DruidRuntimeException;
// import com.alibaba.druid.sql.ast.SQLExpr_ptr;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;

#include "MySqlXAStatement.h"
#include "../../visitor/MySqlASTVisitor.h"
#include "../../../../../utils/StringUtils.h"
#include "../../../../../Exception/DruidRuntimeException.h"

MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::START = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::BEGIN = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::END = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::PREPARE = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::COMMIT = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::ROLLBACK = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));
MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::RECOVER = MySqlXAStatement_XAType_ptr(new MySqlXAStatement_XAType(1 << 0, make_string_ptr("")));

MySqlXAStatement_XAType::MySqlXAStatement_XAType(uint64_t mask, string_ptr name)
{
  this->mask = mask;
  this->name = name;
}

MySqlXAStatement_XAType_ptr MySqlXAStatement_XAType::of(string_ptr typeStr)
{
  if (typeStr == nullptr || typeStr->empty())
  {
    return nullptr;
  }

  string_ptr upper = StringUtils::toUpper(typeStr);
  if (upper->c_str() == "START")
    return MySqlXAStatement_XAType::START;
  else if (upper->c_str() == "BEGIN")
    return MySqlXAStatement_XAType::BEGIN;
  else if (upper->c_str() == "END")
    return MySqlXAStatement_XAType::END;
  else if (upper->c_str() == "PREPARE")
    return MySqlXAStatement_XAType::PREPARE;
  else if (upper->c_str() == "COMMIT")
    return MySqlXAStatement_XAType::COMMIT;
  else if (upper->c_str() == "ROLLBACK")
    return MySqlXAStatement_XAType::ROLLBACK;
  else if (upper->c_str() == "RECOVER")
    return MySqlXAStatement_XAType::RECOVER;
  else
  {
    std::string tmp = "not support xa type ";
    tmp += typeStr->c_str();
    throw new DruidRuntimeException(make_string_ptr(tmp));
  }
}

MySqlXAStatement_XAType_ptr MySqlXAStatement::getType()
{
  return type;
}

void MySqlXAStatement::setType(MySqlXAStatement_XAType_ptr type)
{
  this->type = type;
}

SQLExpr_ptr MySqlXAStatement::getId()
{
  return id;
}

void MySqlXAStatement::setId(SQLExpr_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(MySqlXAStatement));
  }
  this->id = x;
}

void MySqlXAStatement::accept0(MySqlASTVisitor_ptr v)
{
  if (v->visit(SharedObject(MySqlXAStatement)))
  {
    acceptChild(v, id);
  }
  v->endVisit(SharedObject(MySqlXAStatement));
}
